##
rm(list=ls())
gc()


library(data.table)
library(ggplot2)
library(dplyr)
library(stringr)
library(tidyr)
files = c('results/placebo-results-2016-2020.Rdata',
'results/current-results-2016-2020.Rdata')

results = rbindlist(lapply(files, FUN=function(file){

  
  load(file)
  
  models = ls()[grepl('Model',ls())]
  
  l = rbindlist(lapply(models, FUN=function(x){
   # print(x)
    m = get(x)
    out = as.data.table(m$coefficient)
    out[,Covariate:=rownames(m$coefficient)]
    out[,Model:=x]
    out[,N:=m$N]
    out[,`R-Squared`:=m$r.squared]
    out[,`Adjusted R-Squared`:=m$adj.r.squared]
    out[,Sigma:=m$sigma]
    out[,FStat:=m$fstat]
    if('Cluster s.e.'%in%names(out)){
      out[,SE:=`Cluster s.e.`]
      out[,SE.type:='Cluster']
      out[,`Cluster s.e.`:=NULL]
    } else {
      out[,SE:=`Robust s.e`]
      out[,SE.type:='Robust']
      out[,`Robust s.e`:=NULL]
    }


return(out)

  }))
  
  l[grepl('2008',file),Year1:='2008']
  l[grepl('2008',file),Year2:='2012']
l[grepl('2020',file),Year1:='2016']
l[grepl('2020',file),Year2:='2020'] 
l[!grepl('2008',file)&!grepl('2020',file),Year1:='2012']
l[!grepl('2008',file)&!grepl('2020',file),Year2:='2016']
l[,file:=file]

return(l)
}))


results = results[Covariate%in%c('DemSpExpDiff_nohh','RepSpExpDiff_nohh')]

results[grepl('Dems',Model),Subset:='Democrats']
results[grepl('Reps',Model),Subset:='Republicans']
results[grepl('Oths',Model),Subset:='Non-partisans']

results[grepl('placebo',file),Placebo.Year:=substr(Model,19,22)]
results[!grepl('placebo',file),Placebo.Year:='2020']

results[grepl('DemSpExp',Model),Exposure.Type:='Effect of\nexposure to\nDemocrats on\nDemocratic\nregistration']
results[grepl('RepSpExp',Model),Exposure.Type:='Effect of\nexposure to\nRepublicans on\nRepublican\nregistration']



holder = results[1:12,]
holder[1,] = list(NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,'Democrats','...','Effect of\nexposure to\nDemocrats on\nDemocratic\nregistration')
holder[2,] = list(NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,'Democrats','...','Effect of\nexposure to\nRepublicans on\nRepublican\nregistration')
holder[3,] = list(NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,'Democrats','2016','Effect of\nexposure to\nDemocrats on\nDemocratic\nregistration')
holder[4,] = list(NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,'Democrats','2016','Effect of\nexposure to\nRepublicans on\nRepublican\nregistration')

holder[5,] = list(NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,'Republicans','...','Effect of\nexposure to\nDemocrats on\nDemocratic\nregistration')
holder[6,] = list(NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,'Republicans','...','Effect of\nexposure to\nRepublicans on\nRepublican\nregistration')
holder[7,] = list(NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,'Republicans','2016','Effect of\nexposure to\nDemocrats on\nDemocratic\nregistration')
holder[8,] = list(NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,'Republicans','2016','Effect of\nexposure to\nRepublicans on\nRepublican\nregistration')

holder[9,] = list(NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,'Non-partisans','...','Effect of\nexposure to\nDemocrats on\nDemocratic\nregistration')
holder[10,] = list(NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,'Non-partisans','...','Effect of\nexposure to\nRepublicans on\nRepublican\nregistration')
holder[11,] = list(NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,'Non-partisans','2016','Effect of\nexposure to\nDemocrats on\nDemocratic\nregistration')
holder[12,] = list(NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,'Non-partisans','2016','Effect of\nexposure to\nRepublicans on\nRepublican\nregistration')


results=rbind(results,holder)
results$Placebo.Year=str_replace(results$Placebo.Year,'20','\'')
results$Placebo.Year=factor(results$Placebo.Year,levels = str_replace(as.character(c(2012:2016, '...', '2020')),'20','\''))


colors = c(Democrats = "#377EB8", Republicans = "#E41A1C", `Non-partisans` = 'purple')


g = ggplot(results, aes(y = Estimate, x = Placebo.Year, color = Subset))+
  geom_point(size = 5)+
  geom_errorbar(aes(ymin = Estimate-qnorm(.975)*SE, ymax = Estimate+qnorm(.975)*SE), size = 1, position = position_dodge(width=0.75), width = 0)+
  geom_hline(yintercept = 0, linetype = 'dashed')+
  theme_bw()+
  facet_grid(Exposure.Type~Subset)+
  geom_vline(xintercept='\'16', linetype='dotted')+
  scale_color_manual(values=colors)+
  ylab('Effect on partisan registration')+
  xlab('Placebo year')+
  guides(color = 'none',shape='none')+
  theme(text = element_text(size=15,family='serif'),strip.text.y = element_text(size=12, angle = 0), legend.title = element_blank(), legend.position = 'bottom')
  
ggsave(plot = g, filename = 'figures/Fig5.png', dpi= 300, width = 11, height = 5, units = 'in')

gc()

